<?php

if (! function_exists('b_d3downloads_list_show') ) {
	function b_d3downloads_list_show( $options )
	{
		global $xoopsConfig ;
		$db =& Database::getInstance() ;
		$myts =& MyTextSanitizer::getInstance() ;

		require_once dirname( dirname(__FILE__) ).'/include/common_functions.php' ;
		$mydirname = empty( $options[0] ) ? 'd3downloads' : $options[0] ;
		$categories = trim( @$options[1] ) === '' ? array() : array_map( 'intval' , explode( ',' , $options[1] ) ) ;
		$selected_order = empty( $options[2] ) || ! in_array( $options[2] , b_d3downloads_list_order() ) ? 'd.date DESC' : $options[2] ;
		$max_entry = $options[3] ;
		$date_format = $options[4] ;
		$show_body = $options[5] ;
		$this_template = empty( $options[6] ) ? 'db:'.$mydirname.'_block_list.html' : trim( $options[6] ) ;

		if( preg_match( '/[^0-9a-zA-Z_-]/' , $mydirname ) ) die( 'Invalid mydirname' ) ;

		$whr_cat4read = "d.cid IN (".implode(",",d3download_can_read( $mydirname ) ).")" ;
		// categories
		if( $categories === array() ) {
			$whr_categories = '1' ;
			$categories4assign = '' ;
		} else {
			$whr_categories = 'd.cid IN ('.implode(',',$categories).')' ;
			$categories4assign = implode(',',$categories) ;
		}
		$sql = "SELECT d.lid, d.cid, d.title, d.url, d.homepage, d.version, d.size, d.platform, d.logourl, d.description, d.html, d.smiley, d.br, d.xcode, d.submitter, d.date, d.hits, d.rating, d.votes, d.comments, c.title AS ctitle FROM ".$db->prefix( $mydirname."_downloads" )." d LEFT JOIN ".$db->prefix( $mydirname."_cat" )." c ON d.cid=c.cid WHERE d.visible = '1'  AND ( $whr_cat4read ) AND ($whr_categories) ORDER BY $selected_order";
		$result = $db->query( $sql, $max_entry, 0 );
		$downdata = array();
		while ( $myrow = $db->fetchArray( $result ) ) {
			$lid4assign = intval( $myrow['lid'] );
			$cid4assign = intval( $myrow['cid'] );
			$submitter = intval( $myrow['submitter'] );
			$html = intval( $myrow['html'] );
			$smiley = intval( $myrow['smiley'] );
			$xcode = intval( $myrow['xcode'] );
			$br = intval( $myrow['br'] );
			$body = $myts->displayTarea( $myrow['description'] , $html, $smiley, $xcode, 1, $br ) ;
			if ( strstr ( $body , '[pagebreak]' ) ){
				$desc_string = explode( '[pagebreak]', $body , 2 ) ;
				$body4assign = $desc_string[0].'<br /><div align="right"><a href="'.XOOPS_URL.'/modules/'.$mydirname.'/index.php?page=singlefile&amp;cid='.$cid4assign.'&lid='.$lid4assign.'">'._MD_D3DOWNLOADS_LANG_SHOWSINGLEFILE.'</a></div>';
			} else {
				$body4assign = $body ;
			}
			$downdata[] = array(
				'lid' => $lid4assign ,
				'cid' => $cid4assign ,
				'category' => $myts->makeTboxData4Show( $myrow['ctitle']  ) ,
				'title' => $myts->makeTboxData4Show( $myrow['title'] ) ,
				'url' => $myts->makeTboxData4Show( $myrow['url'] ) ,
				'homepage' => $myts->makeTboxData4Show( $myrow['homepage'] ) ,
				'version' => $myts->makeTboxData4Show( $myrow['version'] ) ,
				'size' =>  b_d3downloads_list_PrettySize( intval( $myrow['size'] ) ),
				'platform' => $myts->makeTboxData4Show( $myrow['platform'] ) ,
				'logourl' => $myts->makeTboxData4Show( $myrow['logourl'] ) ,
				'description' => htmlspecialchars( $myrow['description'] , ENT_QUOTES  ) ,
				'body' =>  $body4assign,
				'postname' =>  d3download_postname( $submitter ),
				'updated' => formatTimestamp( intval( $myrow['date'] ) , $date_format, $xoopsConfig['default_TZ'] ),
				'date' => intval( $myrow['date'] ) ,
				'hits' => intval( $myrow['hits'] ) ,
				'rating' => intval( $myrow['rating'] ) ,
				'votes' => intval( $myrow['votes'] ) ,
				'comments' => intval( $myrow['comments'] ) ,
			);
		}
		if( ! empty( $downdata ) ){
			$block['download'] = $downdata ;
			$block['mydirname'] = $mydirname ;
			$block['mod_url'] = XOOPS_URL.'/modules/'.$mydirname ;
			$block['show_body'] = $show_body;

			if( empty( $options['disable_renderer'] ) ) {
				require_once XOOPS_ROOT_PATH.'/class/template.php' ;
				$tpl =& new XoopsTpl() ;
				$tpl->assign( 'block' , $block ) ;
				$ret['content'] = $tpl->fetch( $this_template ) ;
				return $ret ;
			} else {
				return $block ;
			}
		}
	}
}

if (! function_exists('b_d3downloads_list_edit') ) {
	function b_d3downloads_list_edit( $options )
	{
		$mydirname = empty( $options[0] ) ? 'd3downloads' : $options[0] ;
		$categories = trim( @$options[1] ) === '' ? array() : array_map( 'intval' , explode( ',' , $options[1] ) ) ;
		$selected_order = empty( $options[2] ) || ! in_array( $options[2] , b_d3downloads_list_order() ) ? 'd.date DESC' : $options[2] ;
		$max_entry = empty( $options[3] ) ? 10 : intval( $options[3] )  ;
		$date_format = empty( $options[4] ) ? 'Y/m/d' :  htmlspecialchars ( $options[4] , ENT_QUOTES ) ;
		$show_body = empty( $options[5] ) ? false : true ;
		$this_template = empty( $options[6] ) ? 'db:'.$mydirname.'_block_list.html' : trim( $options[6] ) ;

		if( preg_match( '/[^0-9a-zA-Z_-]/' , $mydirname ) ) die( 'Invalid mydirname' ) ;

		require_once XOOPS_ROOT_PATH.'/class/template.php' ;
		$tpl =& new XoopsTpl() ;
		$tpl->assign( array(
			'mydirname' => $mydirname ,
			'categories' => $categories ,
			'categories_imploded' => implode( ',' , $categories ) ,
			'order_options' => b_d3downloads_allowed_order() ,
			'selected_order' => $selected_order ,
			'max_entry' => $max_entry ,
			'date_format' => $date_format ,
			'show_body' => $show_body ,
			'this_template' => $this_template ,
		) ) ;
		return $tpl->fetch( 'db:'.$mydirname.'_blockedit_list.html' ) ;
	}
}

if (! function_exists('b_d3downloads_list_order') ) {
	function b_d3downloads_list_order()
	{
		return array(
			'd.hits' ,
			'd.hits DESC' ,
			'd.rating' ,
			'd.rating DESC' ,
			'd.votes' ,
			'd.votes DESC' ,
			'd.title' ,
			'd.title DESC' ,
			'd.date' ,
			'd.date DESC' ,
		) ;
	}
}

if (! function_exists('b_d3downloads_list_PrettySize') ) {
	function b_d3downloads_list_PrettySize( $size )
	{
		$mb = 1024*1024;
		if ( $size > $mb ) {
			$mysize = sprintf ("%01.2f",$size/$mb) . " MB";
		} elseif ( $size >= 1024 ) {
			$mysize = sprintf ("%01.2f",$size/1024) . " KB";
		} else {
		    $mysize = sprintf( _MB_D3DOWNLOADS_NUMBYTES , $size);
		}
		return $mysize;
	}
}

?>